package com.substation.ui.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.annotation.IdRes;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.alibaba.fastjson.JSON;
import com.greenentity.Area;
import com.greenentity.Task;
import com.greenentity.Unit;
import com.previewlibrary.ZoomMediaLoader;
import com.substation.R;
import com.substation.greendao.AreaDao;
import com.substation.greendao.DefectDao;
import com.substation.greendao.UnitDao;
import com.substation.ui.fragment.DefectFragment;
import com.substation.ui.fragment.InspectionFragment;
import com.substation.ui.utils.MyImageLoader;
import com.substation.utils.GreenDaoManager;
import com.substation.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class PatroDetailActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    int select = 0;
    InspectionFragment inspectionFragment;
    DefectFragment defectFragment;
    @BindView(R.id.radioButton2)
    RadioButton radioButton2;
    Task task;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_patro_detail;
    }

    @Override
    protected void initView() {
        ZoomMediaLoader.getInstance().init(new MyImageLoader());
        selecte(select);
        radioGroup.check(R.id.radioButton1);
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    protected void initData() {
        String unitContent = (String) SPUtils.get(context, "unit", "");
        Unit unit = JSON.parseObject(unitContent, Unit.class);
        String taskContent = (String) SPUtils.get(context, "task", "");
        task = JSON.parseObject(taskContent, Task.class);
        toolbar.setTitle(unit.getTourUnit());
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshData();
    }

    public void refreshData() {
        if (defectFragment != null) {
            defectFragment.getDatas();
        }
        if (inspectionFragment != null) {
            inspectionFragment.setDatas();
        }
        radioButton2.setText("设备缺陷  ( " + GreenDaoManager.getInstance().getNewSession().getDefectDao().queryBuilder()
                .where(DefectDao.Properties.SubsGuid.eq(task.getSubsGuid()), DefectDao.Properties.WorkGuid.eq(task.getWorkGuid())).count() + " )");
    }

    /**
     * 设置fragment显示以及切换
     *
     * @param i
     */
    private void selecte(int i) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction, i);
        switch (i) {
            case 0:
                if (inspectionFragment == null) {
                    inspectionFragment = new InspectionFragment();
                    fragmentTransaction.add(R.id.content, inspectionFragment);
                } else {
                    fragmentTransaction.show(inspectionFragment);
                }
                break;
            case 1:
                if (defectFragment == null) {
                    defectFragment = new DefectFragment();
                    fragmentTransaction.add(R.id.content, defectFragment);
                } else {
                    fragmentTransaction.show(defectFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction ft, int i) {
        if (inspectionFragment != null && i != 0)
            ft.hide(inspectionFragment);
        if (defectFragment != null && i != 1)
            ft.hide(defectFragment);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        switch (i) {
            case R.id.radioButton1:
                select = 0;
                break;
            case R.id.radioButton2:
                select = 1;
                break;
        }
        selecte(select);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_next, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.local:
                List<String> guid = new ArrayList<>();
                for (Area a : GreenDaoManager.getInstance().getmDaoSession().getAreaDao().queryBuilder().where(AreaDao.Properties.WorkGuid.eq(task.getWorkGuid())).list()) {
                    guid.add(a.getTswcGuid());
                }
                Unit unit = JSON.parseObject((String) SPUtils.get(context, "unit", ""), Unit.class);
                List<Unit> units = GreenDaoManager.getInstance().getNewSession().getUnitDao().queryBuilder()
                        .where(UnitDao.Properties.RouteMap.eq(unit.getRouteMap())
                                , UnitDao.Properties.TswcGuid.in(guid), UnitDao.Properties.ShowSortNo.gt(unit.getShowSortNo()))
                        .orderAsc(UnitDao.Properties.ShowSortNo).list();
                if (units.size() > 0) {
                    Unit unit1 = units.get(0);
                    SPUtils.put(context, "unit", JSON.toJSONString(unit1));
                    inspectionFragment.setDatas();
                    toolbar.setTitle(unit1.getTourUnit());
                } else {
                    showMsg("已经是该区域最后一个单元了");
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
